
import { createSlice } from "@reduxjs/toolkit";
import {
    getChildrenListAPI,
    getChildrenCourseListAPI,
    getLearnConditionAPI,
    getPartList,
    getInfo
} from '@/apis/parent.js'


const parentStore = createSlice({
    name:'parent',
    initialState:{
        childrenList:[],
        chilrenCourseList:[],
        partList:[],
        learnCondition:{},
        info:{}
    },
    reducers:{
        setChildrenList(state,action){
            state.childrenList = action.payload;
        },
        setChilrenCourseList(state,action){
            state.chilrenCourseList = action.payload;
        },
        setPartList(state,action){
            state.partList = action.payload;
        },
        setLearnCondition(state,action){
            state.learnCondition = action.payload;
        },
        setInfo(state,action){
            state.info = action.payload;
        }
    }
})

const {
    setChildrenList,
    setChilrenCourseList,
    setPartList,
    setLearnCondition,
    setInfo
} = parentStore.actions;

const parentReducer = parentStore.reducer;

const fetchGetChildrenList = (token) => {
    return async function(dispatch){
        const res = await getChildrenListAPI(token);
        console.log("get childrenlist",res.data);
        dispatch(setChildrenList(res.data));
    }
}


const fetchGetChildrenCourseList = (student_id,token) => {
    return async function(dispatch){
        const res = await getChildrenCourseListAPI(student_id,token);
        console.log("get CourseList",res.data);
        dispatch(setChilrenCourseList(res.data));
    }
}

const fetchGetPartList = (data,token) => {
    return async function(dispatch){
        const res = await getPartList(data,token);
        console.log("get PartList",res.data);
        dispatch(setPartList(res.data));
    }
}

const fetchGetLearnCondition = (data,token) => {
    return async function(dispatch){
        const res = await getLearnConditionAPI(data,token);
        console.log("get Condition",res.data);
        dispatch(setLearnCondition(res.data));
    }
}

const fetchGetInfo = (token) => {
    return async function(dispatch){
        const res = await getInfo(token);
        console.log("get Info",res.data);
        dispatch(setInfo(res.data));
    }
}


export {
    setChildrenList,
    setChilrenCourseList,
    setPartList,
    setLearnCondition,
    setInfo,
    fetchGetChildrenCourseList,
    fetchGetChildrenList,
    fetchGetInfo,
    fetchGetLearnCondition,
    fetchGetPartList,
}


export default parentReducer;